<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="en">
<title>Insert title here</title>
<head>
<script type="text/javascript" src="<%=request.getContextPath()%>/common/jquery-1.8.2.min.js"></script>
<jsp:include page="/common/easyui.jsp"></jsp:include>
<link rel="stylesheet" href="<%=request.getContextPath()%>/common/bootstrap/css/bootstrap.css" type="text/css"></link>
<script type="text/javascript" src="<%=request.getContextPath() %>/common/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
$(function(){
	$('#tablebox').datagrid({    
	    url: '<%=request.getContextPath()%>/user/getItemsPublic.do',
	    onDblClickCell: function(index,field,value){
			$(this).datagrid('beginEdit', index);
				var ed = $(this).datagrid('getEditor', {index:index,field:field});
				$(ed.target).focus();
		},fitColumns:true,
		 columns:[[    
	        {field:'id',title:'Item ID',width:100},    
	        {field:'itemname',title:'itemname',width:100,editor:'text'},
	        {field:'ispublic',title:'状态',width:100,editor:{type:'combobox',
							options:{
								valueField:'ispublic',
								textField:'name',
								data:[{'ispublic':1,'name':'公开'},{'ispublic':0,'name':'不公开'}],
								required:true
							}},formatter:function(value,row,index){return "公开";}
	        },
			{field:'action',title:'Action',width:80,align:'center',
				formatter:function(value,row,index){
					if (row.editing){
						var s = '<a href="javascript:void(0)" onclick="saverow(this)">Save</a> ';
						var c = '<a href="javascript:void(0)" onclick="cancelrow(this)">Cancel</a>';
						return s+c;
					} else {
						var e = '<a href="javascript:void(0)" onclick="editrow(this)">Edit</a> ';
						var d = '<a href="javascript:void(0)" onclick="deleterow(this)">Delete</a>';
						return e+d;
					}
				}
			},
			 {field:'cat',title:'cat',width:100,formatter:function(value,row,index){
			 	var a = '<a href="javascript:void(0)" onclick="openWin(this)">catContent</a>';
				return a;
			}}
	    ]],
	    onEndEdit:function(index,row){
			$.ajax({
				type:"post",
				url:"<%=request.getContextPath()%>/user/updateItem.do",
				data:{id:row.id,itemname:row.itemname,ispublic:row.ispublic},
				success:function(){
					$("#tablebox").datagrid('reload');
				}
			});
		},
		onBeforeEdit:function(index,row){
			var sessionid = ${sessionScope.user.id};
			if(sessionid==row.userid){
				row.editing = true;
				$(this).datagrid('refreshRow', index);
			}else{
				alert("只能编辑本人的")
				$('#tablebox').datagrid('endEdit',null);
			}
		},
		onAfterEdit:function(index,row){
			row.editing = false;
			$(this).datagrid('refreshRow', index);
		},
		onCancelEdit:function(index,row){
			row.editing = false;
			$(this).datagrid('refreshRow', index);
		}
	    
	});  
});
	function getRowIndex(target){
		var tr = $(target).closest('tr.datagrid-row');
		return parseInt(tr.attr('datagrid-row-index'));
	}
	function editrow(target){
		$('#tablebox').datagrid('beginEdit', getRowIndex(target));
	}
	function deleterow(target){
		$.messager.confirm('Confirm','Are you sure?',function(r){
			if (r){
				$('#tablebox').datagrid('deleteRow', getRowIndex(target));
			}
		});
	}
	function saverow(target){
		$('#tablebox').datagrid('endEdit', getRowIndex(target));
	}
	function cancelrow(target){
		$('#tablebox').datagrid('cancelEdit', getRowIndex(target));
	}
	
	function formatDate(value,row,index){
		var date = new Date(value);
		return date.getFullYear()+"-"+(date.getMonth()-1)+"-"+date.getDay();
	}
	
	function openWin(target){
		var index = getRowIndex(target)
		var row = $("#tablebox").datagrid("getRows")[index];
		$("#win").window("open");
		var sessionid = ${sessionScope.user.id};
		if(sessionid!=row.userid){
			$("#saveBut").unbind("click",saveItem);
			$("#saveBut").bind("click",msgSave);
		}else{
			$("#saveBut").unbind("click",msgSave);
			$("#saveBut").bind("click",saveItem);
		}
		$("#itemForm").form('load',{
			id:row.id,
			itemname:row.itemname,
			ispublic:row.ispublic,
			itemcontent:row.itemcontent,
			itemduty:row.itemduty,
			startdate:formatDate(row.startdate),
			enddate:formatDate(row.enddate)
		});
	}
	
	function saveItem(){
		var data = $("#itemForm").serialize();
		$.ajax({
			type:"post",
			url:"<%=request.getContextPath()%>/user/updateItem.do",
			data:data,
			success:function(){
				$("#win").window("close");
				$("#tablebox").datagrid('reload');
			}
		});
	}
	function msgSave(){
		alert("只可以修改你的信息");
	}
</script>
<style type="text/css">
	a{color: #000}
</style>
</head>
<body>
<table id="tablebox"title="项目列表"  >   
</table>  
<div id="win" class="easyui-window" title="My Window" style="width:600px;height:400px"   
        data-options="iconCls:'icon-save',modal:true,closed:true">
        	<div class="modal-header" id='header'>
					<h4 class="modal-title" id="myModalLabel">新增项目</h4>
			</div>
			<div class="modal-body">
		        <form class="form-horizontal" id="itemForm">
		        	<input type="hidden" name="id"/>
								<fieldset>
									<div class="control-group">
										<label class="control-label" for="input01">项目名称</label>
										<div class="controls">
											<input type="text" class="form-control" id="input01" name="itemname" placeholder="字母，数字，汉字皆可">
										</div>
									</div>
									 <div class="control-group">  
								        <label for="ispublic" class="control-label" >是否公开:</label>  
								         <div> 
								         <div class="controls">
								        <select class="easyui-combobox" name="ispublic" style="width:200px;">   
										    <option value="0">不公开</option>   
										     <option value="1">公开</option>   
										</select> 
										 </div>
										</div>
								    </div> 
			    
									<div class="control-group">
										<label class="control-label" for="itemcontent">项目描述</label>
										<div class="controls">
											<textarea class="form-control" rows="4" id="itemcontent" placeholder="描述项目的业务流程" name="itemcontent"></textarea>
										</div>
									</div>
									<div class="control-group">
										<label class="control-label" for="textarea02">个人职责</label>
										<div class="controls">
											<textarea class="form-control" rows="3" id="textarea02" placeholder="描述自己在项目中负责的功能" name="itemduty"></textarea>
										</div>
									</div>
								    <div class="control-group">
										<label class="control-label" for="startdate">起始时间</label>
										<div class="controls">
								        <input id="startdate" class="form-control" name="startdate" data-options="required:true" type="text"  onfocus="WdatePicker({maxDate:'#F{$dp.$D(\'enddate\')}',dateFmt:'yyyy-MM-dd'})">
										</div> 
								    </div>
								     
								     <div class="control-group">
										<label class="control-label" for="enddate">结束时间</label>
										<div class="controls">
								    	<input id="enddate"  class="form-control" name="enddate" type="text" data-options="required:true"  onfocus="WdatePicker({minDate:'#F{$dp.$D(\'startdate\')}',maxDate:'%y-%M-%d',dateFmt:'yyyy-MM-dd'})">
										</div>
								    </div>  
								</fieldset>
							</form>
					</div>
       	   			<div class="modal-footer">
						<button type="button" class="btn btn-success" id="saveBut" >Save</button>
       	   			</div>
			</div>  
</body>
</html>